package com.yuyou.activity.group.job;

import com.yuyou.activity.group.job.Async.AsyncService;
import com.yuyou.activity.group.service.entity.ActivityGroupJob;
import com.yuyou.activity.group.service.entity.ActivityGroupbuyOrder;
import com.yuyou.activity.group.service.service.IActivityGroupJobService;
import com.yuyou.activity.group.service.service.IActivityGroupbuyOrderService;
import com.yuyou.activity.group.service.service.IActivityGroupbuyService;
import com.yuyou.activity.util.enums.ActivityGroupJobEnums;
import com.yuyou.activity.util.util.Sequence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
public class GroupSPSchedule {

    @Autowired
    private IActivityGroupbuyOrderService orderService;

    @Autowired
    private IActivityGroupJobService activityGroupJobService;

    @Autowired
    private IActivityGroupbuyService activityGroupbuyService;

    @Autowired
    private IActivityGroupbuyOrderService activityGroupbuyOrderService;

    @Autowired
    private AsyncService asyncService;

    /**
     * 更新活动状态
     * 每隔50秒执行任务
     */
    @Scheduled(cron="0/60 * * * * ?")
    public void updateActivityStatus(){
        activityGroupbuyService.updateFailActivity();
    }


    /**
     * 更新拼团成功的用户的通知状态状态
     * 每隔1分钟执行任务
     */
    @Scheduled(cron="0 0/5 * * * ?")
    public void updateUserStatus(){
        List<ActivityGroupbuyOrder> list = orderService.findNotUpdateUser();
        List<ActivityGroupJob> jobs = new ArrayList<>();
        Date now = new Date();
        list.forEach(activityGroupbuyOrder -> {
            ActivityGroupJob job = new ActivityGroupJob();
            job.setId(Sequence.getSequence().nextId());
            job.setfId(activityGroupbuyOrder.getActivityGroupbuyOrderId());
            job.setCreateTime(now);
            job.setType(ActivityGroupJobEnums.type.GROUP_SHIPIN.getValue());
            job.setStatus(ActivityGroupJobEnums.status.init.getValue());
            job.setTag(ActivityGroupJobEnums.tag.GROUP_SHIPIN.getValue());

            jobs.add(job);
        });

        activityGroupJobService.insertJobShipin(list , jobs , 1);


    }

    /**
     * 添加shipin.com同步任务（成功拼团的订单）
     * 每隔1分钟执行任务
     */
    @Scheduled(cron="0 0/1 * * * ?")
    public void addSysnJob(){
        List<ActivityGroupbuyOrder> list = orderService.getNeedSyncOrder();
        List<ActivityGroupJob> jobs = new ArrayList<>();
        Date now = new Date();
        list.forEach(activityGroupbuyOrder -> {
            ActivityGroupJob job = new ActivityGroupJob();
            job.setId(Sequence.getSequence().nextId());
            job.setfId(activityGroupbuyOrder.getActivityGroupbuyOrderId());
            job.setCreateTime(now);
            job.setType(ActivityGroupJobEnums.type.SHIPIN.getValue());
            job.setStatus(ActivityGroupJobEnums.status.init.getValue());
            job.setTag(ActivityGroupJobEnums.tag.SHIPIN.getValue());

            jobs.add(job);
        });

        activityGroupJobService.insertJobShipin(list , jobs , 2);


    }

    /**
     * 处理异常订单（超过时间没有支付成功回馈）
     */
    @Scheduled(cron="0 0/5 * * * ?")
    public void orderCheck(){
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MINUTE , -15);//15分钟前
        Date bigTime = calendar.getTime();

        calendar.setTime(new Date());
        calendar.add(Calendar.HOUR , -2);//两个小时前
        Date smallTime = calendar.getTime();

        List<ActivityGroupbuyOrder> list = activityGroupbuyOrderService.getAllNeedCheckOrder(bigTime , smallTime);
        list.forEach(activityGroupbuyOrder -> asyncService.AsyncOrderCheck(activityGroupbuyOrder.getActivityGroupbuyOrderId()));


    }

}
